{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "page_content='这是第一段内容。' metadata={'Header 1': '主标题', 'Header 2': '副标题1'}\n",
      "page_content='这是第二段内容。' metadata={'Header 1': '主标题', 'Header 2': '副标题2'}\n"
     ]
    }
   ],
   "source": [
    "from langchain_text_splitters import MarkdownHeaderTextSplitter\n",
    "\n",
    "markdown_document = \"\"\"\n",
    "# 主标题\n",
    "\n",
    "## 副标题1\n",
    "\n",
    "这是第一段内容。\n",
    "\n",
    "## 副标题2\n",
    "\n",
    "这是第二段内容。\n",
    "\"\"\"\n",
    "\n",
    "headers_to_split_on = [\n",
    "    (\"#\", \"Header 1\"),\n",
    "    (\"##\", \"Header 2\"),\n",
    "]\n",
    "\n",
    "markdown_splitter = MarkdownHeaderTextSplitter(\n",
    "                                                headers_to_split_on, \n",
    "                                                strip_headers=True, # strip_headers=False 保留标题\n",
    "                                                return_each_line=False, \n",
    "                                                )    \n",
    "splits = markdown_splitter.split_text(markdown_document)\n",
    "\n",
    "for split in splits:\n",
    "    print(split)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
